package model;

/**
 *
 * @author Dustin
 */
public class ReplacementSelection {
    public final static int M = 3;
    private int INIndex = 0;
    private int OUTIndex = 1;
    private int[] IN;
    private int[] OUT;
    private Heap h;

    public ReplacementSelection(int[] in) {
        h = new Heap(M);
        IN = in;
    }

    public void replaceSelect() {
        for(int i = 0; i < M; i++) {
            h.insert(IN[INIndex++]);
        }
        while(INIndex < IN.length) {
            h.buildHeap();
            int minimum = h.removeMinimum();
            System.out.println("Wrote out " + minimum + " in run number " + OUTIndex);
            int next = IN[INIndex++];
            if(next >= minimum) {
                h.insert(next);
            } else {
                h.insertInDeadSpace(next);
                if(h.getSize() == 0) {
                    System.out.println("End of run " + OUTIndex++);
                    h.rebuiltHeap();
                }
            }
        }
        System.out.println("End of tape");

        while(!h.isEmpty()) {
            System.out.println("Wrote out " + h.removeMinimum() + " in run number " + OUTIndex);
        }
        System.out.println("End of run " + OUTIndex++);
        
        h.rebuiltHeap();
        while(!h.isEmpty()) {
            System.out.println("Wrote out " + h.removeMinimum() + " in run number " + OUTIndex);
        }
    }
}
